//
//  DiaryViewController.m
//  土巴兔
//
//  Created by 千锋 on 15/12/31.
//  Copyright (c) 2015年 IOS1503薛凯. All rights reserved.
//

#import "DiaryViewController.h"
#import "SecondDiaryViewController.h"
#import "Default.h"
#import "AFNetworking.h"
#import "DiaryModel.h"
#import "DiaryFirstTableViewCell.h"
@interface DiaryViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UISegmentedControl *segment;

@property (strong,nonatomic)UITableView* tableview;
@property (strong,nonatomic)NSMutableArray* dataArray;
@end

@implementation DiaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _segment.layer.cornerRadius=16;
    _segment.clipsToBounds=YES;
    // Do any additional setup after loading the view from its nib.
    _segment.selectedSegmentIndex=0;
    self.navigationController.navigationBarHidden=YES;
    
    [self createTableVIew];
    [self getData];
}
- (void)viewWillAppear:(BOOL)animated{
    if (_segment.selectedSegmentIndex==1) {
        _segment.selectedSegmentIndex-=1;
    }
}
- (IBAction)PressSegment:(UISegmentedControl *)sender {
    switch ([sender selectedSegmentIndex]) {
        case 1:
            [self.navigationController pushViewController:[[SecondDiaryViewController alloc] init] animated:NO];
            break;
            
        default:
            break;
    }
}
#pragma mark- 创建表格
- (void)createTableVIew{
    _tableview=[[UITableView alloc] initWithFrame:CGRectMake(0, 55, DEVICEWIDTH, DEVICEHEIGHT-55)];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    [_tableview registerClass:[DiaryFirstTableViewCell class] forCellReuseIdentifier:@"DiaryFirstTableViewCell"];
    [self.view addSubview:_tableview];
}
#pragma mark- 表格协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return _dataArray.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DiaryFirstTableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:@"DiaryFirstTableViewCell"];
    [cell relayoutWithModel:_dataArray[indexPath.row]];
  
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}
#pragma mark- 获取数据
- (void)getData{
    _dataArray=[[NSMutableArray alloc] initWithCapacity:0];
    AFHTTPRequestOperationManager* manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSString* urlStr=@"http://mobileapi.to8to.com/index.php";
    NSMutableDictionary* paras=[NSMutableDictionary dictionaryWithDictionary:@{@"action":@"list",@"perPage":@"15",@"appid":@"15",@"to8to_token":@"5577450_75ff93ea5d8fae3c8a5a0ef1cb6dc1db",@"systemversion":@"22",@"imei":@"569eb1056c38e1e8",@"appversion":@"3.6.0",@"appostype":@"1",@"version":@"2.5",@"uid":@"5577450",@"type":@"0",@"page":@"1",@"channel":@"%E9%AD%85%E6%97%8F%E5%BA%94%E7%94%A8%E4%B8%AD%E5%BF%83",@"paging":@"1",@"model":@"live"}];
    [paras setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"to8to_token"] forKey:@"to8to_token"];
    [paras setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"uid"] forKey:@"uid"];
        [manager POST:urlStr parameters:paras success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSDictionary* dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray* arr=[dic objectForKey:@"data"];
            for (NSDictionary* item in arr) {
                DiaryModel* model=[[DiaryModel alloc] initWithDic:item];
                [_dataArray addObject:model];
                [_tableview reloadData];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
}
@end
